package contexto;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Properties;

import resource.ResourceUtil;
import business.BusinessUtil;


public class InitialContext {
	private static Hashtable<?, ?>params;
	//Nome do Arquivo JNDI
	public static final String JNDI_FILE="file.jndi";
	//Nome do arquivo JNDI para Lookup do User Transaction
	public static final String JNDI_USER_TRANSACTION_FILE = "file.jndi.usertransaction";
	
	static{
		try{
			//Obtem o arquivo JNDI
			InputStream input = getJndiFile(JNDI_FILE);
			//Carrega as propriedades
			Properties properties = new Properties();
			properties.load(input);
			params = (Hashtable<?, ?>)properties;
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	//Contrutor default
	private InitialContext(){
		
	}
	

	private static InputStream getJndiFile(String jndiFile) {
		String file = ResourceUtil.getConfigurationProperty(jndiFile);
		ClassLoader loader = BusinessUtil.getRemoteClassLoader();
		return loader.getResourceAsStream(file);
	}
	
	public static Hashtable<?, ?>getParams(String jndiFile){
		//Obtem arquivo jndi
		InputStream input = getJndiFile(JNDI_FILE);
		//Carrega as propriedades
		Properties properties = new Properties();
		try{
			properties.load(input);
		}catch (IOException e) {
			throw new RuntimeException("" + JNDI_FILE);
			
		}
		return (Hashtable<?, ?>)properties;
	}
}
